package com.tarena.lbs.business.service.impl;

import com.alibaba.nacos.common.utils.CollectionUtils;
import com.github.pagehelper.PageInfo;
import com.mysql.cj.xdevapi.Result;
import com.tarena.lbs.base.protocol.exception.BusinessException;
import com.tarena.lbs.base.protocol.pager.PageResult;
import com.tarena.lbs.business.dao.repository.BusinessRepository;
import com.tarena.lbs.business.pojo.param.BusinessParam;
import com.tarena.lbs.business.pojo.po.BusinessPO;
import com.tarena.lbs.business.pojo.query.BusinessQuery;
import com.tarena.lbs.business.pojo.vo.BusinessStoreVO;
import com.tarena.lbs.business.pojo.vo.BusinessVO;
import com.tarena.lbs.business.service.BusinessService;
import com.tarena.lbs.common.enums.PassportResultEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;


@Slf4j
@Service
public class BusinessServiceImpl implements BusinessService {
    @Autowired
    private BusinessRepository businessRepository;
    @Override
    public PageResult<BusinessVO> pageList(BusinessQuery query) {
        log.debug("开始查询商家列表，入参：{}", query);
        PageResult<BusinessVO> voPage = new PageResult<>();
        // 调用 adminRepository 的 gePages 方法进行分页查询，获取包含管理员持久化对象的分页信息
        PageInfo<BusinessPO> pageInfo = businessRepository.pagesList(query);
        //pageNo pageSize total
        // 将分页信息中的页码设置到分页结果对象中
        voPage.setPageNo(pageInfo.getPageNum());
        // 将分页信息中的每页数量设置到分页结果对象中
        voPage.setPageSize(pageInfo.getPageSize());
        // 将分页信息中的总记录数设置到分页结果对象中
        voPage.setTotal(pageInfo.getTotal());
        // 声明一个存储管理员视图对象的列表
        List<BusinessVO> vos = null;
        // 从分页信息中获取管理员持久化对象列表
        List<BusinessPO> pos = pageInfo.getList();
        // 判断持久化对象列表是否不为空
        if (CollectionUtils.isNotEmpty(pos)){
            vos=pos.stream().map(po->{
                BusinessVO vo = new BusinessVO();
                BeanUtils.copyProperties(po,vo);
                return vo;
            }).collect(Collectors.toList());
        }
        voPage.setObjects(vos);
        return voPage;
    }
@Override
    public BusinessStoreVO busiStoreDetail(Integer businessId) throws BusinessException{
    log.debug("开始查询商家详情，入参：{}", businessId);
    if (businessId==null){
        log.error("商家ID不能为空");
        throw new BusinessException(PassportResultEnum.EMPTY_NOT_NULL);
    }
        BusinessPO po = businessRepository.getById(businessId);
        if (po==null){
            log.error("商家ID不存在");
            throw new BusinessException(PassportResultEnum.NOT_FOUND_BUSINESS);
        }
        BusinessStoreVO vo = new BusinessStoreVO();
        BeanUtils.copyProperties(po,vo);
        log.debug("查询商家详情成功，入参：{}", vo);
        return vo;
    }

    @Override
    public BusinessVO getBusinessById(Integer id) {
        //通过id查找到商家信息
        BusinessPO po = businessRepository.getBusinessById(id);
        //将po 转化成vo
        BusinessVO vo = null;
        if (po != null) {
            vo = new BusinessVO();
            BeanUtils.copyProperties(po, vo);
        }
        return vo;
    }

    @Override
    public void save(BusinessParam param) throws BusinessException{
        log.debug("获取前端传递的参数：{}", param);
        String businessName = param.getBusinessName();
        Integer industryType = param.getIndustryType();
        String businessHeadName = param.getBusinessHeadName();
        String businessHeadPhone = param.getBusinessHeadPhone();
        String companyAddress = param.getCompanyAddress();
        String bussinessIntroduction = param.getBusinessIntroduction();
        if(businessName==null){
            log.error("商家名称不能为空");
            throw new BusinessException(PassportResultEnum.EMPTY_NOT_NULL);
        }
        if(industryType==null){
            log.error("商家行业类型不能为空");
            throw new BusinessException(PassportResultEnum.EMPTY_NOT_NULL);
        }
        if(businessHeadName==null){
            log.error("负责人姓名不能为空");
            throw new BusinessException(PassportResultEnum.EMPTY_NOT_NULL);
        }
        if(businessHeadPhone==null){
            log.error("负责人电话不能为空");
            throw new BusinessException(PassportResultEnum.EMPTY_NOT_NULL);
        }
        if(companyAddress==null){
            log.error("公司地址不能为空");
            throw new BusinessException(PassportResultEnum.EMPTY_NOT_NULL);
        }
        if(bussinessIntroduction==null){
            log.error("商家简介不能为空");
            throw new BusinessException(PassportResultEnum.EMPTY_NOT_NULL);
        }
        log.debug("开始保存商家信息");
        if(businessRepository.existsByBusinessName(businessName) ||
                businessRepository.existsByCompanyAddress(companyAddress)){
            log.error("商家名称或公司地址已存在");
            throw new BusinessException(PassportResultEnum.BUSINESS_EXISTS);
        }
        log.debug("开始保存商家信息");
        BusinessPO businessPO = param.toPO();
        businessRepository.save(businessPO);
    }

    @Override
    public void updateBusiness(BusinessParam param) {
        BusinessPO po = assembleBusinessPO(param);
        businessRepository.updateBusiness(po);
    }

     // 页面参数转换数据库实体类 ，为updateBusiness的方法
    private BusinessPO assembleBusinessPO(BusinessParam param) {
        // 先查询原记录，保留原有entryTime
        BusinessPO existingPO = businessRepository.getById(param.getId());
        BusinessPO po = new BusinessPO();
        BeanUtils.copyProperties(param, po);
        // 保留原有入驻时间
        po.setEntryTime(existingPO.getEntryTime());
        return po;
    }
}
